package jh.com.jhappbf.work;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ab.activity.AbActivity;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.blankj.utilcode.util.TimeUtils;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.ui.State;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author : wwh
 * e-mail  : 953889276@qq.com
 * time    : 2019-6-22 16:20:59
 * desc    :
 */
public class WorkQueryActivity extends AbActivity {

    @BindView(R.id.textView)
    TextView textView;
    private String mStartTime = "";
    private String mStartDate = "";
    private String mEndTime = "";
    private String mEndDate = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_work_query);

        ButterKnife.bind(this);
        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("工作查询");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        View rightViewMore = mInflater.inflate(R.layout.title_right_btn, null);
        mAbTitleBar.addRightView(rightViewMore);
        Button mTitleRightBtn = rightViewMore.findViewById(R.id.title_right_btn);
        mTitleRightBtn.setOnClickListener(v -> {
            Intent intent = new Intent(WorkQueryActivity.this, WorkSettingActivity.class);
            startActivityForResult(intent, 0x0001);
        });

        Intent intent = getIntent();
        if (intent != null) {
            mStartDate = intent.getStringExtra("startDate");
            mStartTime = intent.getStringExtra("startTime");
            mEndDate = intent.getStringExtra("endDate");
            mEndTime = intent.getStringExtra("endTime");
        }

        getWorkInfo();
    }

    private void getWorkInfo() {
        String methodName = "getWorkInfo";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("startDateTime");
        param.add("endDateTime");
        param.add("driver");
        if (mStartDate.isEmpty() || mStartTime.isEmpty()) {
            mStartDate = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()));
            mStartTime = "00:00:00";
        }

        if (mEndDate.isEmpty() || mEndTime.isEmpty()) {
            mEndDate = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()));
            mEndTime = "23:59:59";
        }

        value.add(mStartDate + " " + mStartTime);
        value.add(mEndDate + " " + mEndTime);

        value.add(State.mName);
        Listener<String> listener = response -> {
            JSONObject jsonObject = JsonHelper.getJsonObject(response);
            if (jsonObject != null) {
                String result = jsonObject.optString("result");
                if ("false".equalsIgnoreCase(result)) {
                    Toast.makeText(WorkQueryActivity.this, "查询异常", Toast.LENGTH_SHORT).show();
                    textView.setText("查询异常");
                    return;
                }
            }
            List<JSONObject> jsonObjects = JsonHelper.getJsonObjects(response);
            int size = jsonObjects.size();
            textView.setText("铲车司机：" + State.mName + "\n"
                    + "铲车编号：" + State.mCompanyName + "\n"
                    + "开始时间：" + mStartDate + " " + mStartTime + "\n"
                    + "结束时间：" + mEndDate + " " + mEndTime + "\n"
                    + "工作数量：" + size + " 车"
            );

        };
        ErrorListener errorListener = volleyError -> Toast.makeText(WorkQueryActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
        VolleyCallWebService webService = new VolleyCallWebService(methodName, param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(this).addRequest(webService);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            mStartDate = data.getStringExtra("startDate");
            mStartTime = data.getStringExtra("startTime");
            mEndDate = data.getStringExtra("endDate");
            mEndTime = data.getStringExtra("endTime");
            getWorkInfo();
        }
    }
}
